package com.huiyujiaoyu.powertime.ui.my;


import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.huiyujiaoyu.base.AppContext;
import com.huiyujiaoyu.base.BaseFragment;
import com.huiyujiaoyu.http.HttpApi;
import com.huiyujiaoyu.http.HttpMethods;
import com.huiyujiaoyu.powertime.R;
import com.huiyujiaoyu.powertime.model.IndexItemModel;
import com.huiyujiaoyu.powertime.model.SimpleListBean;
import com.huiyujiaoyu.powertime.ui.home.ArticleDetailsActivity;
import com.huiyujiaoyu.powertime.ui.home.SearchActivity;
import com.huiyujiaoyu.powertime.ui.home.ShiPinDetailActivity;
import com.huiyujiaoyu.powertime.ui.home.VoiceDetailActivity;
import com.huiyujiaoyu.util.ActivityUtil;
import com.huiyujiaoyu.util.AnimatorUtil;
import com.huiyujiaoyu.util.Constant;
import com.huiyujiaoyu.util.DisplayUtil;
import com.huiyujiaoyu.util.GlideUtil;
import com.huiyujiaoyu.util.StringUtils;
import com.huiyujiaoyu.widget.CustomLoadMoreView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static android.R.attr.data;
import static com.huiyujiaoyu.powertime.R.id.rb_content;


/**
 * 首页
 * Created by yujunlong on 2016/12/5.
 */

public class BaseListFragment extends BaseFragment {
    @BindView(R.id.recycle)
    RecyclerView mXrView;
    @BindView(R.id.swipe)
    SwipeRefreshLayout mSwipe;

    private BaseQuickAdapter<IndexItemModel, BaseViewHolder>  adapter;

    private List<IndexItemModel> datas;
    private int page, type,contenttype,layid;

    public static BaseListFragment newInstance(int type,int contenttype) {
        Bundle args = new Bundle();
        args.putInt(Constant.DATA,type);
        args.putInt(Constant.DATA2,contenttype);
        BaseListFragment fragment = new BaseListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.layout_swirecy;
    }

    @Override
    protected void userBundle(Bundle savedInstanceState) {
        super.userBundle(savedInstanceState);
    }

    @Override
    protected void initView() {
        super.initView();
        type = getArguments().getInt(Constant.DATA);
        contenttype = getArguments().getInt(Constant.DATA2);
        switch (type) {
            case 1:
                layid = R.layout.cell_hot_video_more;
                break;
            case 2:
                switch (contenttype) {
                    case 1:
                        layid = R.layout.cell_coupon_no_use;
                        break;
                    case 2:
                        layid = R.layout.cell_coupon_use;
                        break;
                    case 3:
                        layid = R.layout.cell_coupon_time_out;
                        break;
                }
                break;
        }
        datas = new ArrayList<>();
        adapter = new BaseQuickAdapter<IndexItemModel, BaseViewHolder>(layid, datas) {
            @Override
            protected void convert(BaseViewHolder helper, IndexItemModel item) {
                switch (type) {
                    case 1:
                        GlideUtil.loadImg((ImageView) helper.getView(R.id.iv_pic),StringUtils.getFullUrl(item.cover));
                        helper.setText(R.id.tv_zhagnjie,"共"+item.chapter_count+"章");
                        helper.setText(R.id.tv_item_title,item.title);
                        helper.setText(R.id.tv_author,item.column_title);
                        helper.setText(R.id.tv_read_count,item.view_num+"");
                        helper.setText(R.id.tv_comment_count,item.comment_num+"");
                        ImageView imageView = helper.getView(R.id.iv_contenttype);
                        imageView.setImageResource(StringUtils.getResId2(contenttype));
                        DisplayUtil.setDrawbleLeft(mContext, (TextView) helper.getView(R.id.tv_read_count),StringUtils.getResId2(contenttype),10);
                        break;
                    case 2:
                            helper.setText(R.id.tv_mian_e,"￥"+item.money);
                            helper.setText(R.id.tv_date,item.starttime+"-"+item.deadline);
                        break;
                }

            }
        };
        mXrView.setLayoutManager(new LinearLayoutManager(mContext));
        mXrView.setHasFixedSize(true);
        adapter.setLoadMoreView(new CustomLoadMoreView());
        mXrView.setAdapter(adapter);
        mSwipe
                .setColorSchemeResources(R.color.blue_,
                        R.color.blue_, R.color.blue_,
                        R.color.blue_);
        mSwipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadMore();
            }
        }, mXrView);
        mSwipe.setRefreshing(true);
        refresh();
    }


    @Override
    protected void initListener() {
        super.initListener();
        mXrView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                IndexItemModel indexItemModel = datas.get(position);
                switch (type) {
                    case 1:

                        break;
                    case 2:
                        if(contenttype==1){
                            ((MyYHQActivity) getActivity()).setResult(indexItemModel.id,indexItemModel.money);
                        }
                        break;
                }


            }
        });

    }

    @Override
    public void onNext(Object o, int httpcode) {
        super.onNext(o, httpcode);
        switch (httpcode) {
            case HttpApi.HTTP_COLLECTION:
                SimpleListBean   bean = (SimpleListBean) o;
                doData(bean.list);
                break;
            case HttpApi.HTTP_COUPONS:
                bean = (SimpleListBean) o;
                switch (contenttype) {
                    case 1:
                        doData(bean.unused);
                        break;
                    case 2:
                        doData(bean.used);
                        break;
                    case 3:
                        doData(bean.expired);
                        break;
                }

                break;
        }
    }

    private void getData() {
        switch (type) {
            case 1:
                HttpMethods.getInstance().collections(mContext, getComp(), this, contenttype,page);
                break;
            case 2:
                HttpMethods.getInstance().coupons(mContext, getComp(), this, contenttype,page);
                break;
        }

    }

    private void loadMore() {
        if (mSwipe.isEnabled()) {
            mSwipe.setEnabled(false);
        }
        page++;
        getData();
    }

    private void refresh() {
        if (mContext == null)
            return;
        if (adapter == null) {
            return;
        }
        if (adapter.isLoadMoreEnable()) {
            adapter.setEnableLoadMore(false);
        }
        page = 1;
        getData();
    }

    public void doData(List<IndexItemModel> data) {
        if (page == 1) {
            datas.clear();
            // 设置加载更多能用
            if (mSwipe.isRefreshing()) {
                mSwipe.setRefreshing(false);
            }
            datas.addAll(data);
            adapter.notifyDataSetChanged();
            adapter.setEnableLoadMore(true);
            Log.e(TAG, "page == 1");
        } else {
            if (data == null || data.size() == 0) {
                adapter.loadMoreEnd();
            } else {
                datas.addAll(data);
                adapter.notifyDataSetChanged();
                adapter.loadMoreComplete();
            }
            if (!mSwipe.isEnabled())
                mSwipe.setEnabled(true);
            Log.e(TAG, "page != 1");
        }
        Log.e(TAG, "page==" + page);
    }
}
